

function submitOrder() {
    //alert("提交订单-----");

    //封装订单信息
        //用户信息
    var pros = [];
    var custStr = localStorage.getItem("customer");
    var customer = eval("("+ custStr +")");
    //订单总价
    var totalMoney = $("#total_price b").html();
    if(customer == null){
        alert("用户登陆超时，请重新登录");
        window.location.href = "login/login.html";
    }
    var cart = sessionStorage.getItem("cart");
    var proInfo = eval("("+ cart +")").products;
    //console.log(proInfo);
    for(var i=0;i<proInfo.length;++i){
        //console.log($("#" + proInfo[i].productQR).hasClass("pitch_on"));
        if($("#" + proInfo[i].productQR).hasClass("pitch_on")){
            var value = $("#" + proInfo[i].productQR).parent().children(".div_right").children(".zi").html();
            proInfo[i].rentDays = value;
            proInfo[i].price = proInfo[i].rentMoney;
            pros.push(proInfo[i]);
            //alert(proInfo[i].productQR);
        }
    }

    //console.log(pros);



    if(pros.length <= 0){
        alert("请选择要租用的商品");
    }
    else{
        var proStr = JSON.stringify(pros);
        $.ajax({
            url:'/orderInfo!submitOrder',
            type:'post',
            data:{'custStr':custStr,
                'proStr':proStr,
                'totalMoney':totalMoney
            },
            //dataType:"json",
            success:function(data){
                //alert(data);
                //随便的显示一下传回的数据喽
                if(data == "insert_success"){
                    alert("押金支付成功，请等待收货！");
                    window.location.reload(true);
                }
                else
                    alert("租用失败！");
            }
        });
    }

}

function remove() {
    //封装订单信息
    //用户信息
    if(confirm("确认删除吗？")){
        var pros = [];
        var custStr = localStorage.getItem("customer");
        var customer = eval("("+ custStr +")");
        if(customer == null){
            alert("用户登陆超时，请重新登录");
            window.location.href = "login/login.html";
        }
        var cart = sessionStorage.getItem("cart");
        var proInfo = eval("("+ cart +")").products;
        //console.log(proInfo);
        for(var i=0;i<proInfo.length;++i){
            //console.log($("#" + proInfo[i].productQR).hasClass("pitch_on"));
            if($("#" + proInfo[i].productQR).hasClass("pitch_on")){

                pros.push(proInfo[i]);
                //alert(proInfo[i].productQR);
            }
        }

        console.log(pros);

        if(pros.length <= 0){
            alert("请选择要删除的商品");
        }else{
            var proStr = JSON.stringify(pros);
            $.ajax({
                url:'/rentCart!deleteItem',
                type:'post',
                data:{'accountNo':customer.accountNo,
                    'proStr':proStr
                },
                //dataType:"json",
                success:function(data){
                    //alert(data);
                    //随便的显示一下传回的数据喽
                    if(data == "success"){
                        alert("删除成功");
                        window.location.reload();
                    }
                }
            });
        }
    }

}

function seleItem(data){
    var su = $(data).attr("aem");
    var carts_id=$(data).attr("cart_id");
    var totalH = $("#total_price b").text(); /* 合计金额  */
    //alert(totalH);
    if(su == 0){
        /* 单选商品  */
        if($(data).hasClass("pitch_on")){
            /*去该店全选*/
           // $(data).parents("ul").siblings(".select").find("em").removeClass("pitch_on");
            /*去底部全选*/
           // $("#all_pitch_on").removeClass("pitch_on");
            $(data).removeClass("pitch_on");
            reduceMod($(data),totalH);
            cart_id[carts_id]="";
            delete cart_id[carts_id];
        }else{
            $(data).addClass("pitch_on");
         //   var n = $(data).parents("ul").children().find(".pitch_on");
         //   var n1 = $(data).parents("ul").children();
            plusMod($(data),totalH,0,noX);
            cart_id[carts_id]="";
            /*该店商品全选中时*/
           /* if(n.length == n1.length){
                $(data).parents("ul").siblings(".select").find("em").addClass("pitch_on");
            }*/
            /*/!*商品全部选中时*!/
            var fot = $(".commodity_list_box .tite_tim .pitch_on");
            var fot1 = $(".commodity_list_box .tite_tim em");
            if(fot.length == fot1.length)
                $("#all_pitch_on").addClass("pitch_on");*/
        }
    }else{
        /*/!* 全选该店铺  *!/
        if($(data).hasClass("pitch_on")){
            /!*去底部全选*!/
            $("#all_pitch_on").removeClass("pitch_on");
            $(data).removeClass("pitch_on");

            commodityReduceMod($(data),totalH);
            $(data).parent().siblings("ul").find("em").removeClass("pitch_on");
            delete cart_id[carts_id];
        }else{
            commodityReduceMod($(data),totalH);

            $(data).addClass("pitch_on");

            $(data).parent().siblings("ul").find("em").addClass("pitch_on");

            if(plus != NaN && plus != undefined){
                totalH = parseFloat(totalH)-parseFloat(plus);
            }

            commodityPlusMod($(data),totalH);
            cart_id[carts_id]="";
            /!*商品全部选中时*!/
            var fot = $(".commodity_list_box .tite_tim .pitch_on");
            var fot1 = $(".commodity_list_box .tite_tim em");
            if(fot.length == fot1.length)
                $("#all_pitch_on").addClass("pitch_on");

        }*/
    }

    //计算选择数值
    number();
}

$(function () {
    //从localStorage中获取customer
    var custStr = localStorage.getItem("customer");
    var customer = eval("("+ custStr +")");
    var data = "";
    var url;
    if(customer == null){
        alert("用户登陆超时，请重新登录");
        sessionStorage.removeItem("cart");
        window.location.href = "login/login.html";
    }
    var accountNo = customer.accountNo;
   // alert(accountNo);
    url = "rentCart!showRentCart";
    data += "accountNo=" + accountNo;
    sendRequest("post",url,data,showRentCartBack);
});

function showRentCartBack(result) {
    if(result == "\"emptyCart\""){

        alert("租物车为空，请返回");
        window.location.href = "index.html";
    }

    sessionStorage.setItem("cart",eval("(" + result + ")"));
    var rs = eval("("+ eval("("+ result +")") +")");
    console.log(rs);
    var proInfo = rs.products;
    var str = "";
    for(var i=0;i<proInfo.length;++i){
        str += "\t\t\t\t\t<li class=\"select\" >\n" +
            "\t\t\t\t\t\t<em aem=\"0\" carts_id=\""+ proInfo[i].productQR + "\" id=\""+ proInfo[i].productQR +"\" onclick='seleItem(this)'></em>\n" +
            "\t\t\t\t\t\t<img src=\"data:image/jpg;base64,"+proInfo[i].productPhoto+"\" />\n" +
            "\t\t\t\t\t\t<div class=\"div_center\" onclick='updateItem(this)'>\n" +
            "\t\t\t\t\t\t\t<h4>"+proInfo[i].productName+"</h4>\n" +
            "\t\t\t\t\t\t\t<span>"+proInfo[i].introduction+"</span>\n" +
            "\t\t\t\t\t\t\t<p class=\"now_value\">￥<b class=\"qu_su\">"+proInfo[i].rentPrice+"</b>/天</p>\n" +
            "\t\t\t\t\t\t\t<p class=\"now_value\">押金:￥<b class=\"rent_money\">"+proInfo[i].rentMoney+"</b></p>\n" +
            "\t\t\t\t\t\t</div>\n" +
            "\t\t\t\t\t\t<div class=\"div_right\">\n" +
            "\t\t\t\t\t\t\t<i onclick=\"reducew(this)\">-</i>\n" +
            "\t\t\t\t\t\t\t<span class=\"zi\" >"+ proInfo[i].rentDays +"</span>\n" +
            "\t\t\t\t\t\t\t<input type=\"hidden\" id='rentTime' value=\""+ proInfo[i].rentTime +"\">\n" +
            "\t\t\t\t\t\t\t<i onclick=\"plusw(this)\">+</i>\n" +
            "\t\t\t\t\t\t</div>\n" +
            "\t\t\t\t\t</li>\n";
    }
    $(".commodity_box .commodity_list .commodity_list_term").html(str);
}

function updateItem(data) {

    var custStr = localStorage.getItem("customer");
    var accountNo = eval("("+ custStr +")").accountNo;
    var productQR = $(data).parent().children("em").attr("id");
    var data = window.btoa(encodeURIComponent("productQR=" + productQR + "&accountNo=" + accountNo));
    //console.log(data);
    window.location.href = "updateRentCart.html?" + data;

}